﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using frontend_wcf.WCFService;
using System.IO;

namespace frontend_wcf
{
    public partial class FrmAddData : Form
    {
        private ServiceClient service;
        private string currentTable;
        public FrmAddData(ServiceClient service, string tableName)
        {
            this.service = service;
            this.currentTable = tableName;
            InitializeComponent();

            List<string> list = service.getStructure(currentTable).ToList<string>();

            int y = 10;
            int textbox_x = 0;
            int label_x = 10;
            int width = 0;
            foreach (string row in list)
            {
                string[] tmp = row.Split(',');
                string name = tmp[0];
                string type = tmp[1];

                if (name.Equals("id"))
                    continue;

                // labels
                Label tmp_label = new Label();
                tmp_label.AutoSize = true;
                tmp_label.Text = string.Format("{0} ({1}):", name, type);
                tmp_label.Location = new Point(label_x, y);
                this.Controls.Add(tmp_label);

                // textbox
                TextBox tmp_text = new TextBox();
                bool blob = false;
                if (type.ToLower().Contains("blob"))
                {
                    tmp_text.Name = string.Format("{0},BLOB", name);
                    blob = true;
                }
                else
                {
                    tmp_text.Name = name;
                }
                if (tmp_label.Right > textbox_x)
                    textbox_x = tmp_label.Right;
                tmp_text.Location = new Point(textbox_x, y);
                tmp_text.Size = new System.Drawing.Size(180, 20);
                this.Controls.Add(tmp_text);

                
                //Button
                if (blob)
                {
                    Button tmp_button = new Button();
                    tmp_button.Name = "btn_Browse" + tmp_text.Name;
                    tmp_button.Size = new Size(75, 23);
                    tmp_button.Text = "Browse";
                    tmp_button.Location = new Point(tmp_text.Right, y);
                    tmp_button.Click += new EventHandler(btn_Browse_Click);
                    this.Controls.Add(tmp_button);
                }


                y = tmp_text.Bottom+5;
            }

            bool button_exists = false;
            foreach (Control cont in Controls)
            {
                if (cont.GetType() == typeof(TextBox))
                {
                    cont.Left = textbox_x;
                }
                if (cont.GetType() == typeof(Button) && cont.Text == "Browse")
                {
                    cont.Left = textbox_x+190;
                    button_exists = true;
                }
            }

            btn_AddData.Left = 10;
            btn_AddData.Top = y;

            btn_Cancel.Left = btn_AddData.Right + 10;
            btn_Cancel.Top = y;

            this.Width = textbox_x + 180 + 30;
            if (button_exists)
                this.Width += 100;
            this.Height = btn_AddData.Bottom + 40;
        }

        private void frmAddData_Load(object sender, EventArgs e)
        {
        }

        private void btn_Browse_Click(object sender, EventArgs e)
        {
            DialogResult dresult = openFileDialog.ShowDialog();
            if (dresult == System.Windows.Forms.DialogResult.OK)
            {
                Button btn = (Button)sender;
                string txtName = btn.Name.Substring(10);
                TextBox txt_value = (TextBox)getControlByName(txtName);
                txt_value.Text = openFileDialog.FileName;
            }
        }

        private Control getControlByName(string name)
        {
            return Controls.Find(name, true)[0];

        }

        private void btn_AddData_Click(object sender, EventArgs e)
        {
            Dictionary<string, object> dict = new Dictionary<string, object>();
            foreach (Control cont in Controls)
            {
                if (cont.GetType() == typeof(TextBox))
                {
                    string name = cont.Name;
                    string value = cont.Text;
                    if (name.Contains("BLOB"))
                    {
                        name = name.Split(',')[0];
                        string fileName = cont.Text;
                        long FileSize;
                        byte[] rawData;
                        FileStream fs;
                        fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                        FileSize = fs.Length;

                        rawData = new byte[FileSize];
                        fs.Read(rawData, 0, Convert.ToInt32(FileSize));
                        fs.Close();

                        dict.Add(name, rawData);
                    }
                    else
                    {
                        dict.Add(name, value);
                    }
                }
            }
            service.addData(currentTable, dict);
        }
    }
}
